﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Core.IO;
using System.IO;
using System.Data;

namespace Agile.Engine.Utils
{
    /// <summary>
    /// IO资源 工具类
    /// </summary>
    public class IOUtils
    {
        /// <summary>
        /// 获取资源句柄
        /// </summary>
        /// <param name="uri">资源定位</param>
        /// <returns></returns>
        public static IResource ResourceHandler(string uri)
        {
            return new ConfigurableResourceLoader().GetResource(uri);
        }

        /// <summary>
        /// 获取输入流
        /// </summary>
        /// <param name="uri">资源定位</param>
        /// <returns></returns>
        public static Stream InputStream(string uri)
        {
            return ResourceHandler(uri).InputStream;
        }

        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="path"></param>
        public static void CreateDirectory(string path)
        {
            var start = 0;
            var end = path.LastIndexOf('\\');

            while (start < end)
            {
                start = path.IndexOf('\\', ++start);
                var temp = path.Substring(0, start);
                if (!Directory.Exists(temp))
                {
                    Directory.CreateDirectory(temp);
                }
            }

        }

        //#region Gzip Compress
        ///// <summary>
        ///// 压缩byte
        ///// </summary>
        ///// <param name="data"></param>
        ///// <returns></returns>
        //public static byte[] CompressByte(byte[] data)
        //{
        //    byte[] result = null;
        //    using (MemoryStream input = new MemoryStream(data))
        //    {
        //        using (MemoryStream output = new MemoryStream())
        //        {
        //            using (System.IO.Compression.GZipStream zip
        //                = new System.IO.Compression.GZipStream(output, System.IO.Compression.CompressionMode.Compress))
        //            {
        //                input.CopyTo(zip);

        //                result = output.ToArray();
        //            }
        //        }
        //    }
        //    return result;
        //}

        //public static byte[] DeCompressByte(byte[] data)
        //{
        //    byte[] result;

        //    using (MemoryStream input = new MemoryStream(data))
        //    {
        //        using (MemoryStream output = new MemoryStream())
        //        {
        //            using (System.IO.Compression.GZipStream zip
        //                = new System.IO.Compression.GZipStream(input, System.IO.Compression.CompressionMode.Decompress))
        //            {
        //                byte[] buffer = new byte[2048];
        //                int n;
        //                while ((n = zip.Read(buffer, 0, buffer.Length)) > 0)
        //                {
        //                    output.Write(buffer, 0, buffer.Length);
        //                }

        //                //zip.CopyTo(output);
        //                result = output.ToArray();
        //            }
        //        }
        //    }

        //    return result;
        //}

        ///// <summary>
        ///// 压缩流
        ///// </summary>
        ///// <param name="input"></param>
        ///// <param name="output"></param>
        //public static void CompressStream(Stream input, Stream output)
        //{
        //    using (MemoryStream ms = new MemoryStream())
        //    {
        //        input.CopyTo(ms);
        //        var comp = CompressByte(ms.ToArray());
        //        output.Write(comp, 0, comp.Length);
        //    }
        //}

        ///// <summary>
        ///// 压缩流
        ///// </summary>
        ///// <param name="input"></param>
        ///// <param name="output"></param>
        //public static string CompressStream(Stream input)
        //{
        //    using (MemoryStream ms = new MemoryStream())
        //    {
        //        CompressStream(input, ms);
        //        return Convert.ToBase64String(ms.ToArray());
        //    }

        //}

        ///// <summary>
        ///// 解压缩流
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public static byte[] DeCompressString(String data)
        //{
        //    var b = Convert.FromBase64String(data);
        //    return DeCompressByte(b);
        //}

        ///// <summary>
        ///// 解压缩流
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public static void DeCompressStream(Stream input, Stream output)
        //{
        //    using (MemoryStream ms = new MemoryStream())
        //    {
        //        input.CopyTo(ms);
        //        var comp = DeCompressByte(ms.ToArray());
        //        output.Write(comp, 0, comp.Length);
        //    }
        //}

        ///// <summary>
        ///// 压缩数据表
        ///// </summary>
        ///// <param name="datas"></param>
        ///// <returns></returns>
        //public static string CompressData(params DataTable[] datas)
        //{
        //    using (StringWriter sw = new StringWriter())
        //    {
        //        DataSet ds = new DataSet();
        //        ds.Tables.AddRange(datas);
        //        ///写入
        //        ds.WriteXml(sw, XmlWriteMode.WriteSchema);
        //        ///压缩
        //        var compress = CompressByte(Encoding.UTF8.GetBytes(sw.GetStringBuilder().ToString()));
        //        return Convert.ToBase64String(compress);
        //    }
        //}

        ///// <summary>
        ///// 解压数据表
        ///// </summary>
        ///// <param name="data"></param>
        ///// <param name="ds"></param>
        ///// <returns></returns>
        //public static DataSet DeCompressData(string data)
        //{
        //    DataSet ds = new DataSet();
        //    if (string.IsNullOrEmpty(data))
        //        return ds;
        //    var bytes = Convert.FromBase64String(data);
        //    var buffer = DeCompressByte(bytes);
        //    using (StreamReader reader = new StreamReader(Encoding.UTF8.GetString(buffer)))
        //    {
        //        ds.ReadXml(reader, XmlReadMode.ReadSchema);
        //        return ds;
        //    }
        //}

        //#endregion

        /// <summary>
        /// 读取流
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static byte[] ReadStream(string uri)
        {
            byte[] buffer;
            using (Stream s = InputStream(uri))
            {
                MemoryStream ms = new MemoryStream();
                s.CopyTo(ms);
                buffer = ms.ToArray();
            }

            return buffer;
        }

        /// <summary>
        /// 写入输出流,覆盖模式
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="data"></param>
        public static void WriteStream(byte[] data, string uri)
        {
            if (File.Exists(uri))
                File.Delete(uri);

            using (BinaryWriter writer = new BinaryWriter(
                new FileStream(uri, FileMode.Create, FileAccess.Write)))
            {
                writer.Write(data);
                writer.Close();
            }
        }

        /// <summary>
        /// 追加写入输出流
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="data"></param>
        public static void AppendStream(byte[] data, string uri)
        {
            if (File.Exists(uri))
                File.Delete(uri);

            using (BinaryWriter writer = new BinaryWriter(
                new FileStream(uri, FileMode.Append, FileAccess.Write)))
            {
                writer.Write(data);
                writer.Close();
            }
        }
    }
}
